<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use app\admin\controller\Admin;

 
class Flow extends Admin
{

    /**
     * 查询用户流程信息,渲染页面
     * @return [type] [description]
     */
    public function index()
    {   
        //查询流程信息
        $data = $this->flowSee();
        //渲染页面
        return view('admin@flow/index',[
            'data'=>$data
        ]);
    }
     /**
      * 同意流程
      * @param  [type] $id [当前流程存储所在表的ID]
      * @return [type]     [description]
      */
     public function doFlow($id)
     {
        //查询当前用户角色名称及上级角色名称
        $name = find_prole_agree();
        //判断上级角色是否存在
        if($name['nextName'] == null){
            //不存在则完结流程
          $res = $this->agreeFlow($id,$name['nowName'],'',9);
          if ($res > 0) {
              return $this->success('已同意,流程完结!', url('admin/Flow/index'));
          } else {
              return $this->error('同意失败!');
          }
        }else{
            //存在则发送给上级角色
          $res = $this->agreeFlow($id,$name['nowName'],$name['nextName']);
          if ($res > 0) {
              return $this->success('已同意!', url('admin/Flow/index'));
          } else {
              return $this->error('同意失败!');
          }
        }
     }

     /**
      * 不同意流程
      * @param  [type] $id [当前流程存储所在表的ID]
      * @return [type]     [description]
      */
     public function dontFlow($id)
     {
        //查询发起人信息ID
        $role_id = find_prole_disagree($id);
        // var_dump($role_id);die;
        //更新打回流程
        $res = db('role_flow')->where(['details_id'=>$id])->update(['role_id'=>$role_id['rid']]);
        // var_dump($res);die;
        if ($res > 0) {
            return $this->success('已撤回!', url('admin/Flow/index'));
        } else {
            return $this->error('撤回失败!');
        }
     }

     /**
      * 同意流程,更新数据库信息
      * @param  [type] $id     [当前流程存储所在表的ID]
      * @param  [type] $name1  [当前用户角色名]
      * @param  [type] $name   [上级角色名]
      * @param  string $status [流程状态 默认为空]
      * @return [type]         [description]
      */
     public function agreeFlow($id,$name1,$name,$status='')
     {  
        //查询角色ID
        $role_id_new = db('role')->field('id')->where(['name'=>$name])->find();
        $role_id = db('role')->field('id')->where(['name'=>$name1])->find();
        // var_dump($role_id,$role_id_new,$id);die;

        //判断状态死否为空
        if ($status == '') {
            //为空则流程不是完结流程,更新角色ID
             $res = db('role_flow')->where(['role_id'=>$role_id['id'],'details_id'=>$id])->update(['role_id' => $role_id_new['id']]);
            return $res;
        }else{
            //不为空则完结流程,更新流程状态
             $res = db('role_flow')->where(['role_id'=>$role_id['id'],'details_id'=>$id])->update(['flow_status' => $status]);
             return $res;
        }
     }

    public function allFlow()
    {
        return view('admin@flow/allFlow');
    }

    public function endFlow()
    {
        return view('admin@flow/endFlow');
    }

    
    public function flowSee($flow_status='1')
     {  
        $data = [];
        $username = Session::get('admin_user')['username'];
        // var_dump($username);die;
        if ($username == 'admin') {
            $data = db('role_flow')->select();
            // var_dump($data);die;
        }else{
            $uid = db('user')->field('id')->where(['username'=>$username])->find();
            $rid = db('user_role')->field('rid')->where(['uid'=>$uid['id']])->select();
            // var_dump($rid);die;
            foreach ($rid as  $v) {
                $flowname[] = db('role_flow')->where(['role_id'=>$v['rid']])->find();
            }
            foreach ($flowname as  $v) {
                if($v == null){
                    continue;
                }
                $data[] = $v;
            }
        }
       return $data;
     }
}